package entity;

import map.Map;
import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Vector2f;
import constans.ResourceManager;

public class Bullet extends MovingEntity{

	Image img;
	private float damage;
	public boolean herosBullet;
	private boolean detonate = false;
	
	SpriteSheet sheet = new SpriteSheet(ResourceManager.getImage("detonation"), 20,20);
	Animation animation = new Animation(sheet, 50);
	private boolean detonated = false;

	public Bullet(Vector2f position, Vector2f direction, float damage, boolean herosBullet) {
		super(position);
		size = 14;
		speed = 3.2f;
		this.damage = damage;
		movement.set(direction);
		movement.scale(speed);
		if(herosBullet)
			img = ResourceManager.getImage("bullet");
		else
			img = ResourceManager.getImage("UBullet");
		this.herosBullet = herosBullet;
		
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		position.sub(Map.shift);
		if(detonate)
			animation.draw(position.x - size/2, position.y - size/2, size, size);
		else
		{
			img.rotate((float) movement.getTheta() + 90);
			img.draw(position.x - size/2, position.y - size/2, size, size);
			img.rotate(-((float) movement.getTheta() + 90));
		}
		position.add(Map.shift);
	}
	public void update(GameContainer container, int delta) throws SlickException
	{	if(detonate)
		{
			animation.update(delta);
			if(animation.getFrame() == animation.getFrameCount()-1)
				detonated = true;
		}
		else
			position.add(movement);
	}
	public void setTarget(Vector2f t)
	{
		target.set(t);
	}
	public float getDamage()
	{
		return damage;
	}
	public void detonate()
	{
		detonate = true;		
	}
	public boolean isDetonated()
	{
		return detonate;
	}
	public boolean isDead()
	{
		if(detonated)
			return true;
		return false;
	}
}

